package com.example.mobile_function;

import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Common {
	public static String CDate(String dataString) {
		String ConvertData="";
		try {
			if(dataString.contains("Date")){
				dataString = dataString.replace("/Date(", "").replace(")/", "");
				String time = dataString.substring(0, dataString.length() - 5);
				Date date = new Date(Long.parseLong(time));
				SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd hh:mm");
				ConvertData=format.format(date);
			}
			else{
				ConvertData=dataString;
			}

		} catch (Exception e) {
			// TODO: handle exception
			ConvertData="";
		}

		return ConvertData;
	}
	
	public static String CInToString(int IntValue){
		String Value="";
		try {
			Value=String.valueOf(IntValue);
		} catch (Exception e) {
			Value="";
		}
		return Value;
	}
	
	public static boolean IsNum(String Num){
		String reg = "^[0-9]+(.[0-9]+)?$"; 
		return Num.matches(reg);
	}
	
	public static double SringToDoubble(String Value){
		String reg = "^[0-9]+(.[0-9]+)?$"; 
		double Result=0;
		if(Value.matches(reg)){
			Result=Double.parseDouble(Value);
		}
		
		return Result;
	}
	
	public static boolean isReachable(String remoteInetAddr) {
		boolean reachable = false;
		try {
			InetAddress address = InetAddress.getByName(remoteInetAddr);
			reachable = address.isReachable(5000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return reachable;
	}
	
	
	

}